package com.study.kt._00test

fun main() {
    val buyBear = buyBear(4)
    changeBear(buyBear, buyBear)
    println(total + buyBear)
}

const val bearMoney = 2

// 当前盖子数量
var currentLid = 0

// 当前瓶子数量
var currentBottle = 0

// 买酒
fun buyBear(totalMoney: Int): Int {
    currentBottle = totalMoney / bearMoney
    currentLid = totalMoney / bearMoney
    return currentBottle
}

// 换酒
var total = 0
tailrec fun changeBear(bottleNum: Int, lidNum: Int) {
    val changeNum = bottleNum / 2 + lidNum / 4
    total += changeNum
    // 更新数量回收
    val remainBottle = currentBottle % 2
    val remainLid = currentLid % 4
    if (currentBottle + changeNum < 2 && currentLid + changeNum < 4) return
    // 更新剩余酒瓶
    currentBottle = changeNum + remainBottle
    currentLid = changeNum + remainLid
    changeBear(remainBottle + changeNum, remainLid + changeNum)
}
